package cn.szzsi._web;

import cn.szzsi._Config;
import cn.szzsi.common.kit.BackUtil;
import cn.szzsi.common.kit.IdGen;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.SiteConfig;
import cn.szzsi.common.model.zshop.AgentBalance;
import cn.szzsi.common.model.zshop.CompanyInfo;
import cn.szzsi.common.model.zshop.CompanyInfoService;
import cn.szzsi.common.model.zshop.PlateformConfig;
import cn.szzsi.core.kit.CommonUtil;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;
import org.apache.commons.codec.digest.DigestUtils;

import java.math.BigDecimal;
import java.util.Date;

//测试使用
public class AgentController extends BaseWebController<Member> {
    public String agentId = "";

    /**
     * 进入代理商说明页面
     */
    public void indexAgent() {
        Member loginmMember=getLoginMember();
        String phone=loginmMember.getPhone();
        //代理商
        cn.szzsi.common.model.zshop.Member zshopMember= cn.szzsi.common.model.zshop.Member.dao.getByPhone(phone);
        Integer memberType=zshopMember.getType();
        if(zshopMember!=null &&(0==memberType || 2==memberType) && 2==zshopMember.getAgentStates() ){//代理商
            setAttr("zshopMember",zshopMember);
            AgentBalance agentBalance=AgentBalance.dao.getByAgentId(zshopMember.getId());
            setAttr("agentBalance",agentBalance);
            wapTemplatePath("agent_info.html");
        }else{//非代理商
            setAttr("loginmMember",loginmMember);
            wapTemplatePath("join_agent.html");
        }

    }

    /**
     * 申请成为代理商
     * 申请成为代理商(通过个人中心进入)
     *
     */
    public void applyAgent(){
        String name=getPara("name");//申请人姓名
        String phone=getPara("phone");//申请成为代理商的手机号
        String agentCode = IdGen.get().nextId(10);
        cn.szzsi.common.model.zshop.Member zshopMember= cn.szzsi.common.model.zshop.Member.dao.getByPhone(phone);
        if(zshopMember!=null){
            Integer memberType=zshopMember.getType();//类型:0-代理商和商户,1-商户,2-代理商
            Integer agentStates=zshopMember.getAgentStates();//代理商审核状态:1-审核中,2-审核通过,3-审核不通过
            if(0==memberType || 2==memberType){//不是商户
                if(1==agentStates){
                    renderJson(BackUtil.fail("亲,您提交的信息正在审核中"));
                    return;
                }else if(3==agentStates){
                    renderJson(BackUtil.fail("亲,您提交的信息审核不通过"));
                    return;
                }
            }else{//仅仅是商户
                zshopMember.setType(0);//将商户改为多重身份-->增加代理商身份
                zshopMember.setAgentStates(1);//代理商审核中
                zshopMember.setRealName(name);
                zshopMember.setAgentCode(agentCode);
                zshopMember.setAgentApplyDate(new Date());
                zshopMember.update();
            }
        }else {//既不是代理商,又不是商户
            zshopMember=new cn.szzsi.common.model.zshop.Member();
            zshopMember.setId(StrKit.getRandomUUID());
            zshopMember.setRealName(name);
            zshopMember.setUsername(phone);
            zshopMember.setPhone(phone);
            zshopMember.setShopId("0");
            zshopMember.setAgentApplyDate(new Date());
            zshopMember.setRoleId("super");
            zshopMember.setBShoprank("super");
            zshopMember.setType(2);
            zshopMember.setAgentStates(1);
            zshopMember.setAgentCode(agentCode);
            zshopMember.save();
        }
        renderJson(BackUtil.success("您的申请信息已提交"));
    }


    public void applyAgentForm(){
        String code=getPara(0);
        setAttr("code",code);
        PlateformConfig plateformConfig=PlateformConfig.dao.findById(1);
        setAttr("plateformConfig",plateformConfig);
        wapTemplatePath("invite_join_agent.html");
    }
    /**
     * 申请成为代理商
     * 通过别人推荐码进入
     */
    @Clear
    @Before(Tx.class)
    public void applyAgentByCode(){
        String code=getPara("code");//上级推荐码
        Integer agentLevel=getParaToInt("agentLevel");
        //查看上级
        String agentPid="0";
        String agentPids="0";
        if (StrKit.notBlank(code)){
            cn.szzsi.common.model.zshop.Member pidMember=cn.szzsi.common.model.zshop.Member.dao.getByCode(code);
            if(pidMember!=null){
                agentPid=pidMember.getId();
                agentPids=pidMember.getAgentPids();
                if (StrKit.notBlank(agentPids)){
                    agentPids=agentPids+","+agentPid;
                }else{
                    agentPids=agentPid;
                }
            }
        }
        //查看上级
        String name=getPara("name");//申请人姓名
        String phone=getPara("phone");//申请成为代理商的手机号
        String agentCode = IdGen.get().nextId(10);
        cn.szzsi.common.model.zshop.Member zshopMember= cn.szzsi.common.model.zshop.Member.dao.getByPhone(phone);
        if(zshopMember!=null){
            Integer memberType=zshopMember.getType();//类型:0-代理商和商户,1-商户,2-代理商
            Integer agentStates=zshopMember.getAgentStates();//代理商审核状态:1-审核中,2-审核通过,3-审核不通过
            if(0==memberType || 2==memberType){//不是商户
                if(1==agentStates){
                    renderJson(BackUtil.fail("亲,您提交的信息正在审核中"));
                    return;
                }else if(3==agentStates){
                    renderJson(BackUtil.fail("亲,您提交的信息审核不通过"));
                    return;
                }
            }else{//仅仅是商户
                zshopMember.setType(0);//将商户改为多重身份-->增加代理商身份
                zshopMember.setAgentStates(1);//代理商审核中
                zshopMember.setAgentLevel(agentLevel);
                zshopMember.setRealName(name);
                zshopMember.setAgentCode(agentCode);
                zshopMember.setAgentPid(agentPid);
                zshopMember.setAgentPids(agentPids);
                zshopMember.setAgentApplyDate(new Date());
                zshopMember.update();
            }
        }else {//既不是代理商,又不是商户
            zshopMember=new cn.szzsi.common.model.zshop.Member();
            zshopMember.setId(StrKit.getRandomUUID());
            zshopMember.setRealName(name);
            zshopMember.setUsername(phone);
            zshopMember.setPhone(phone);
            zshopMember.setShopId("0");
            zshopMember.setRoleId("super");
            zshopMember.setBShoprank("super");
            zshopMember.setType(2);
            zshopMember.setAgentStates(1);
            zshopMember.setAgentLevel(agentLevel);
            zshopMember.setAgentCode(agentCode);
            zshopMember.setAgentPid(agentPid);
            zshopMember.setAgentPids(agentPids);
            zshopMember.setAgentApplyDate(new Date());
            zshopMember.save();
        }
        renderJson(BackUtil.success("您的申请信息已提交"));
    }

    public void applyBusinessForm(){
        String code=getPara(0);
        setAttr("code",code);
        wapTemplatePath("invite_join_business.html");
    }
    /**
     * 申请成为商户
     * 通过别人推荐码进入
     */
    @Clear
    @Before(Tx.class)
    public void applyBusinessByCode(){
        String realName=getPara("name");//姓名
        String phone=getPara("phone");//手机号
        String weChat=getPara("weChat");//微信号
        String qq=getPara("qq");//
        String product=getPara("product");
        String code=getPara("code");//上级推荐码
        //查看上级
        String pid="0";
        String pids="0";
        if (StrKit.notBlank(code)){
            cn.szzsi.common.model.zshop.Member pidMember=cn.szzsi.common.model.zshop.Member.dao.getByCode(code);
            if(pidMember!=null){
                pid=pidMember.getId();
                pids=pidMember.getPids();
                if (!"0".equals(pids)){
                    pids=pids+","+pid;
                }else{
                    pids=pid;
                }
            }
        }
        //查看上级
        if(getLoginMember()!=null){
            phone=getLoginMember().getPhone();
        }
        if(StrKit.isBlank(weChat)){
            renderJson(Ret.fail("info","微信号不允许为空!"));
            return;
        }
        if(StrKit.isBlank(product)){
            renderJson(Ret.fail("info","产品不允许为空!"));
            return;
        }
        CompanyInfo companyInfo=CompanyInfo.dao.getByPhone(phone);
        if(companyInfo!=null){
            if("1".equals(companyInfo.getStatus())){
                renderJson(Ret.fail("info","该商正在审核中,请耐心等待审核结果!"));
                return;
            }
            if("3".equals(companyInfo.getStatus())){
                renderJson(Ret.fail("info","该商户审核失败,请重新提交审核!"));
                return;
            }

            if("2".equals(companyInfo.getStatus())){
                renderJson(Ret.fail("info","该商户已入住!"));
                return;
            }
        }else{
            cn.szzsi.common.model.zshop.Member member=cn.szzsi.common.model.zshop.Member.dao.getByPhone(phone);
            //1.保存之前,先判断用户身份:类型:0-代理商和商户,1-商户,2-代
            if(member==null){
                member=new cn.szzsi.common.model.zshop.Member();
                member.setId(StrKit.getRandomUUID());
                member.setUsername(phone);
                member.setPhone(phone);
                member.set("shopId", "0");
                member.set("createDate", new Date());
                member.set("roleId", "super");
                member.set("b_shoprank", "super");
                member.setType(1);//商户
                member.setPid(pid);
                member.setPids(pids);
                member.setRealName(realName);
                member.save();
            }else{
                if(2==member.getType()){
                    member.setUsername(phone);
                    member.set("shopId", "0");
                    member.set("createDate", new Date());
                    member.set("roleId", "super");
                    member.set("b_shoprank", "super");
                    member.set("phone", phone);
                    member.setType(0);//将代理商身份更改为双重身份
                    member.update();
                }
            }
            //2.保存商户审核状态
            CompanyInfoService.cis.saveInfo(phone,"",realName,"","1",member.getId(),weChat,qq,product);
            //3.保存siteconfig
            SiteConfig sc=new SiteConfig();
            sc.setUid(member.getId());
            sc.setPhone(phone);
            sc.setShopAlias(phone);
            sc.setCreateDate(new Date());
            sc.save();
            renderJson(BackUtil.success("您的申请信息已提交"));
        }
    }

    /**
     * 推荐二维码-->代理商-->代理商
     */
    @Clear
    public void agentQrcode(){
        String code=getPara(0);
        if(StrKit.isBlank(code)){
            Member member=getLoginMember();
            code=IdGen.get().nextId(10);
            member.setCode(code);
            member.update();
        }
        String url= _Config.p.get("mall_web.url")+"/b/agent/applyAgentForm/"+code;
        renderQrCode(url,180,180);
    }

    /**
     * 推荐二维码-->代理商-->商户
     */
    @Clear
    public void businessQrcode(){
        String code=getPara(0);
        if(StrKit.isBlank(code)){
            Member member=getLoginMember();
            code=IdGen.get().nextId(10);
            member.setCode(code);
            member.update();
        }
        String url= _Config.p.get("mall_web.url")+"/b/agent/applyBusinessForm/"+code;
        renderQrCode(url,180,180);
    }

    public static void main(String[] args) {

        //System.out.println(percent);
        System.out.println(StrKit.getRandomUUID());
    }
}
